package com.test.util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


import org.springframework.web.multipart.MultipartFile;

public class AntUtil {
	
	public static void unzip(MultipartFile multipartFile) throws IOException {
		ZipInputStream zipInputStream=new ZipInputStream(multipartFile.getInputStream(),Charset.forName("GBK"));
		ZipEntry zipEntry=null;
		while((zipEntry=zipInputStream.getNextEntry())!=null) {
			byte[] bytes=new byte[4096];
			int len;
			String fileName=zipEntry.getName();
//			ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
//			if("content.html".equals(fileName)) {
//				BufferedReader br=new BufferedReader(new InputStreamReader(bis,"utf-8"));
//				String line;
//				while((line=br.readLine())!=null) {
//					System.out.println(line);
//				}
//				br.close();
//			}
//			OutputStream outputStream = new FileOutputStream("F://"+fileName);
			if(zipEntry.isDirectory()){

			}else{
				ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				while((len=zipInputStream.read(bytes))>0) {
					outputStream.write(bytes, 0, len);
				}
				byte[] bytes2=outputStream.toByteArray();
				FileOutputStream fileOutputStream=new FileOutputStream("F://"+fileName);
				fileOutputStream.write(bytes2);

				outputStream.close();
				fileOutputStream.close();
			}
			System.out.println("");
			zipInputStream.closeEntry();
		}
		zipInputStream.close();
	}

	public static void unzip1(File file) throws IOException {
		ZipInputStream zipInputStream=new ZipInputStream(new FileInputStream(file),Charset.forName("GBK"));

		ZipEntry zipEntry=null;
		while((zipEntry=zipInputStream.getNextEntry())!=null) {
			byte[] bytes=new byte[4096];
			int len;
			String fileName=zipEntry.getName();

			if(zipEntry.isDirectory()){
				continue;
			}

			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			while((len=zipInputStream.read(bytes))>0) {
				outputStream.write(bytes, 0, len);
			}
			byte[] bytes2=outputStream.toByteArray();

			ByteArrayInputStream bis=new ByteArrayInputStream(bytes2);

			StringBuffer sBuffer=new StringBuffer();

			BufferedReader br=new BufferedReader(new InputStreamReader(bis,"utf-8"));
			String line;
			while((line=br.readLine())!=null) {
				sBuffer.append(line);
				System.out.println(line);
			}
			br.close();


			outputStream.close();

			zipInputStream.closeEntry();
		}
		zipInputStream.close();
	}

	public static void main(String[] args) throws IOException {
//		ZipFile zipFile=new Z
		unzip1(new File("F:\\work\\ziptest\\ziptest.zip"));
	}
}
